package labilabyrinth.level.editor;

import java.util.ArrayList;
import labilabyrinth.activity.GameActivity;
import labilabyrinth.enums.EditMode;
import labilabyrinth.game.GameSurface;
import labilabyrinth.game.R;
import labilabyrinth.serializable.DeadlyHole;
import labilabyrinth.serializable.Hole;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class LevelEditor extends View{
	
	private Bitmap background, deadlyHoleBmp, finishHoleBmp,surface,ball;
	private Canvas canvas;
	public int elementsSize;
	private int width, height;
	private Paint paint;
	public ArrayList<RectF> obstacles;
	public ArrayList<Hole> holes;
	public Point ballPos; 
	public Point finishHolelPos;
	private EditMode mode;
	public  Point defaultBallPos;
	public  Point defaultFinishPos; 
	private int downX,downY,upX,upY;
	private boolean moving = false;
	public EditMode getMode() {
		return mode;
	}


	public void setMode(EditMode mode) {
		this.mode = mode;
	}


	public LevelEditor(Context context) {
		super(context);
		WindowManager windowManager = (WindowManager) context.getSystemService(GameActivity.WINDOW_SERVICE);
		Display display = windowManager.getDefaultDisplay();
		elementsSize =  display.getWidth()/ GameSurface.ELEM_SIZE_FACTOR;
		surface = Bitmap.createBitmap(display.getWidth(), display.getHeight(), Bitmap.Config.ARGB_8888);
		canvas = new Canvas(surface);
		obstacles =  new ArrayList<RectF>();
		holes = new ArrayList<Hole>();
		ballPos = new Point();
		finishHolelPos = new Point();
		mode = EditMode.BALL_POS;
		paint = new Paint();
		paint.setColor(Color.parseColor("#ffdab9"));
		background = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_surface_480x800);
		Bitmap tempBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.hole);
		deadlyHoleBmp = Bitmap.createScaledBitmap(tempBmp,elementsSize+4,elementsSize+4 , true);
		tempBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.finish_hole);
		finishHoleBmp = Bitmap.createScaledBitmap(tempBmp,elementsSize+4,elementsSize+4 , true);
		tempBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.orange_ball);
		ball = Bitmap.createScaledBitmap(tempBmp,elementsSize,elementsSize , true);
		canvas.drawBitmap(background, 0,0, null);
	}
	
	protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
        super.onSizeChanged(xNew, yNew, xOld, yOld);
        width = xNew;
        height = yNew;
        defaultBallPos = new Point(elementsSize*2,elementsSize*2);
        defaultFinishPos = new Point(width/2,height/2);
        ballPos.x = defaultBallPos.x;
        ballPos.y = defaultBallPos.y;
        finishHolelPos.x = defaultFinishPos.x;
        finishHolelPos.y = defaultFinishPos.y;
        setUpBumpers();
    }
	
	public void setUpBumpers(){
		RectF up,left,right,bottom;
		up =  new RectF(0,0,width,elementsSize);
		left = new RectF(0,0,elementsSize,height);
		right = new RectF(width-elementsSize,0,width,height);
		bottom = new RectF(0,height-elementsSize,width,height);
		obstacles.add(up);
		obstacles.add(left);
		obstacles.add(bottom);
		obstacles.add(right);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event){
		int e = event.getAction() & MotionEvent.ACTION_MASK;
		if(e == MotionEvent.ACTION_DOWN){
			downX = (int) event.getX();
			downY = (int) event.getY();
			switch (mode) {
			case BALL_POS:
				ballPos.x = downX;
				ballPos.y = downY;
				break;
			case FINISH_HOLE:
				finishHolelPos.x = downX;
				finishHolelPos.y = downY;
				break;
			case DEADLY_HOLE:
				DeadlyHole d = new DeadlyHole(elementsSize, downX, downY);
				holes.add(d);
				break;
			case OBSTACLE:
				moving = true;
				break;
			default:
				break;
			}
			invalidate();
		}
		if(e == MotionEvent.ACTION_UP){
			
			upX = (int) event.getX();
			upY = (int) event.getY();
			Log.d("lvl","UP: ["+upX+","+upY+"]");
			switch (mode) {
			case OBSTACLE:
				if(moving){
				  drawObstacle();
				  moving = false;
				}
				break;
			default:
				break;
			}
			invalidate();
		}
		return true;
	}
	
	public void drawObstacle(){
		RectF r;
		//slide left
		if(downX > upX){
			int dx = Math.abs(downX-upX);
			r = new RectF(upX,upY,upX+dx,upY + elementsSize);
			obstacles.add(r);
		}
		//slide right
		if(downX < upX){
			int dx = Math.abs(downX-upX);
			r = new RectF(downX,downY,downX+dx,downY + elementsSize);
			obstacles.add(r);
		}
		//slide top
		if(downY > upY){
			int dy = Math.abs(downY-upY);
			r = new RectF(upX,upY,upX+elementsSize,upY + dy);
			obstacles.add(r);
		}
		//slide down
		if(downY < upY){
			int dy = Math.abs(downY-upY);
			r = new RectF(downX,downY,downX+elementsSize,downY + dy);
			obstacles.add(r);
		}
	}
	
	public void clearMap(){
		obstacles.clear();
		setUpBumpers();
		holes.clear();
        ballPos.x = defaultBallPos.x;
        ballPos.y = defaultBallPos.y;
        finishHolelPos.x = defaultFinishPos.x;
        finishHolelPos.y = defaultFinishPos.y;
        invalidate();
	}

	
	public void onDraw(Canvas canvas){
		canvas.drawBitmap(background, 0, 0,null);
		if((ballPos.x!=0)||(ballPos.y!=0))
			canvas.drawBitmap(ball, ballPos.x-(elementsSize/2), ballPos.y-(elementsSize/2), null);
		if((finishHolelPos.x!=0)||(finishHolelPos.y!=0))
			canvas.drawBitmap(finishHoleBmp, finishHolelPos.x-(elementsSize/2), finishHolelPos.y-(elementsSize/2), null);
		for(Hole h:holes)
				canvas.drawBitmap(deadlyHoleBmp, h.getCenter().x-(elementsSize/2),h.getCenter().y-(elementsSize/2) ,null);
		for(RectF r:obstacles)
			canvas.drawRect(r, paint);
	}
}
